package com.groupware.cal.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.RedirectView;

import com.groupware.cal.dao.CalDaoImpl;
import com.groupware.cal.vo.CalVo;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);
	@Autowired
	private CalDaoImpl calDaoImpl;

	@RequestMapping(value = "/calIndex1.do", method = RequestMethod.GET)	//달력 리스트 받아와
	public String cal(HttpServletRequest req, Model model, HttpSession session) {
		String userId =(String) session.getAttribute("userId");
		List<CalVo> list = calDaoImpl.getCal(userId);
		model.addAttribute("list", list);
		System.out.println(list);
		return "calendar/calIndex1";
	}

	@RequestMapping(value = "/insertpro.do", method = RequestMethod.GET) 	//삽입
	public String insert(HttpServletRequest req, Model model ,HttpSession session) {
		CalVo vo = new CalVo();
		String userid = (String)session.getAttribute("userId");
		vo.setEnd(req.getParameter("end"));
		vo.setStart(req.getParameter("start"));
		vo.setTitle(req.getParameter("title"));
		vo.setUserId(userid);
		calDaoImpl.insertCal(vo);
		return "calendar/insertForm";
	}

	@RequestMapping(value = "/update.do", method = RequestMethod.GET)		//수정폼에서 받아오는 값
	public String update(HttpServletRequest req, Model model, HttpSession session)
			throws UnsupportedEncodingException {
		CalVo vo = new CalVo();
		String userid = (String)session.getAttribute("userId");
		vo.setEnd(req.getParameter("end"));
		vo.setStart(req.getParameter("start"));
		vo.setTitle(req.getParameter("title"));
		vo.setNum(Integer.parseInt(req.getParameter("num")));
		vo.setUserId(userid);
		System.out.println(vo);
		model.addAttribute("list", vo);

		return "calendar/updateForm2";
	}

	@RequestMapping(value = "/updatepro.do", method = RequestMethod.GET) 		//수정
	public RedirectView updatepro(HttpServletRequest req, CalVo vo, HttpSession session) {
		vo.setNum(Integer.parseInt(req.getParameter("num")));
		String userid = (String)session.getAttribute("userId");
		vo.setUserId(userid);
		System.out.println(vo.getNum());
		System.out.println("pro "+vo);
		calDaoImpl.updateCal(vo);
		return new RedirectView("calIndex1.do?userId="+userid);
	}
	
	@RequestMapping(value = "/deletepro.do", method = RequestMethod.GET)			//삭제
	public RedirectView delete(HttpServletRequest req, HttpSession session) {
		CalVo vo = new CalVo();
		String userid = (String)session.getAttribute("userId");
		vo.setEnd(req.getParameter("end"));
		vo.setStart(req.getParameter("start"));
		vo.setTitle(req.getParameter("title"));
		vo.setUserId(userid);
		vo.setNum(Integer.parseInt(req.getParameter("num")));
		System.out.println("del"+vo);
		calDaoImpl.deleteCal(vo);
		return new RedirectView("calIndex1.do?userId="+userid);
	}
	@RequestMapping(value = "/insertForm.do", method = RequestMethod.GET)
	public String insertForm(HttpServletRequest req, Model model,CalVo vo, HttpSession session){
		
		return "calendar/insertForm";
	}
	
	@RequestMapping(value = "/updateForm.do", method = RequestMethod.GET)
	public String updateForm(HttpServletRequest req, Model model, HttpSession session)
			throws UnsupportedEncodingException {
		CalVo vo = new CalVo();
		String userid = (String)session.getAttribute("userId");
		vo.setEnd(req.getParameter("end"));
		vo.setStart(req.getParameter("start"));
		vo.setTitle(req.getParameter("title"));
		vo.setNum(Integer.parseInt(req.getParameter("num")));
		vo.setUserId(userid);
		System.out.println(vo);
		model.addAttribute("list", vo);
		return "calendar/updateForm";
	}
}
